/**
 * 实施计划成本
 * Created by douxc on 2015/4/21.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('ProgrammedCostsCtrl', ['$scope', '$location', 'MHService',
      function ($scope, $location, MHService) {

        var feeds = MHService.cacheData(false, {key: 'materialList'});//读取需要显示的数据
        var _feeds = [];

          var temp = MHService.cacheData(false, {key: 'materialCosts'});
          if(temp){
              $scope.siteFunds = temp.siteFund;
              $scope.tax = temp.tax;
              $scope.marketing = temp.marketing;
              $scope.management = temp.management;
              $scope.other = temp.other;
          }else{
              $scope.siteFunds = null;
              $scope.tax = null;
              $scope.marketing = null;
              $scope.management = null;
              $scope.other = null;
          }

        /**
         * 计算材料成本
         */
        for (var obj in feeds) {
//          var len = feeds[obj].length - 1;//num对应的值
          var tf = {
            name: feeds[obj].name,
            costs: feeds[obj].price * parseInt(feeds[obj].num)//单价乘以数量作为成本
          };
          _feeds.push(tf);//需要显示的数据
        }
        $scope.materials = _feeds;
        /**
         * 将所有数据保存，进入下一步计算比例
         */
        $scope.programCostsNext = function () {
          var materialCosts = {
            material: _feeds
          };
          if ($scope.siteFunds) {
            materialCosts.siteFund = parseFloat($scope.siteFunds);
          } else {
            MHService.Toast('现场经费未填');
            return;
          }
          if ($scope.tax) {
            materialCosts.tax = parseFloat($scope.tax);
          } else {
            MHService.Toast('税金未填');
            return;
          }
          if ($scope.marketing) {
            materialCosts.marketing = parseFloat($scope.marketing);
          } else {
            MHService.Toast('营销费未填');
            return;
          }
            if ($scope.management) {
                materialCosts.management = parseFloat($scope.management);
            } else {
                MHService.Toast('管理费未填');
                return;
            }
          if ($scope.other) {
            materialCosts.other = parseFloat($scope.other);
          } else {
            MHService.Toast('其他经费未填');
            return;
          }
          MHService.cacheData(true, {
            key: 'materialCosts',
            value: materialCosts
          });
          $location.path('/quoteSettle');
        };
      }]);
  };
});
